{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "Python自带一个调试器, 在Python 3.7之后甚至成为内置调试器. 这就是PDB. 这是使用Python的用户需要掌握的基本技能. \n",
    "\n",
    "下面我们看一下基本演示(GIF循环播放):\n",
    "\n",
    "![](https://ai.bdstatic.com/file/368702F4ACC64CF59009A3EE348F9765)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "对应代码如下, 大家可以运行起来试一下. \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pdb\n",
    "\n",
    "class MyScrapy:\n",
    "    urls = []\n",
    "\n",
    "    def start_url(self, urls):\n",
    "        pdb.set_trace()\n",
    "        for url in urls:\n",
    "            print(url)\n",
    "            self.urls.append(url)\n",
    "\n",
    "    def parse(self):\n",
    "        pdb.set_trace()\n",
    "        for url in self.urls:\n",
    "            result = self.request_something(url)\n",
    "\n",
    "    def request_something(self, url):\n",
    "        print('requesting...')\n",
    "        data = '''<!DOCTYPE html>\n",
    "<html lang=\"en\">\n",
    "<head>\n",
    "    <meta charset=\"UTF-8\">\n",
    "    <title>Title</title>\n",
    "</head>\n",
    "<body>\n",
    "</body>\n",
    "</html>'''\n",
    "        return data\n",
    "\n",
    "\n",
    "scrapy= MyScrapy()\n",
    "scrapy.start_url([\"http://www.zone7.cn\", \"http://www.zone7.cn\", \"http://www.zone7.cn\", \"http://www.zone7.cn\", ])\n",
    "scrapy.parse()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "下面是详细使用说明, 主要内容参考 [howchoo](https://howchoo.com/g/zgi2y2iwyze/debugging-your-python-code) \n",
    "\n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/adb216d833604fff86cec8d7aa68b5b512e3a6b10d204198a2b37903bd29dcb1)\n",
    "\n",
    "Python具有一个整洁的调试功能（像大多数其他语言一样），在这种情况下非常方便。本指南是快速教程，希望能让您的生活更轻松"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 1. 一个混乱的程序\n",
    "\n",
    "为了本教程的目的，让我们考虑下面的简单程序。\n",
    "\n",
    "该程序采用两个命令行参数并执行加法和减法操作。\n",
    "\n",
    "（让我们假设用户输入有效值，因此我们不是错误处理）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "-2\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "\n",
    "def add(num1=0, num2=0):\n",
    "    return int(num1) + int(num2)\n",
    "\n",
    "def sub(num1=0, num2=0):\n",
    "    return int(num1) - int(num2)\n",
    "\n",
    "def main():\n",
    "    #Assuming our inputs are valid numbers\n",
    "    num1 = 0\n",
    "    num2 = 2\n",
    "    \n",
    "    addition = add(num1, num2)\n",
    "    print (addition)\n",
    "    \n",
    "    subtraction = sub(num1, num2)\n",
    "    print (subtraction)\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 2. pdb来了\n",
    "Python附带了一个名为pdb的有用模块，它基本上是一个交互式源代码调试器。\n",
    "\n",
    "您需要以下几行来实际使用此模块\n",
    "\n",
    "```\n",
    "import pdb\n",
    "pdb.set_trace()\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> <ipython-input-4-4a6fb344b2b4>(15)main()\n",
      "-> addition = add(num1, num2)\n",
      "(Pdb) "
     ]
    },
    {
     "ename": "BdbQuit",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mBdbQuit\u001b[0m                                   Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-4-4a6fb344b2b4>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     19\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     20\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'__main__'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 21\u001b[0;31m     \u001b[0mmain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m<ipython-input-4-4a6fb344b2b4>\u001b[0m in \u001b[0;36mmain\u001b[0;34m()\u001b[0m\n\u001b[1;32m     13\u001b[0m     \u001b[0mnum2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m44\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     14\u001b[0m     \u001b[0mpdb\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mset_trace\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# <-- 这个表示添加断点\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m     \u001b[0maddition\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0madd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnum2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     16\u001b[0m     \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0maddition\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     17\u001b[0m     \u001b[0msubtraction\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msub\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnum2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m<ipython-input-4-4a6fb344b2b4>\u001b[0m in \u001b[0;36mmain\u001b[0;34m()\u001b[0m\n\u001b[1;32m     13\u001b[0m     \u001b[0mnum2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m44\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     14\u001b[0m     \u001b[0mpdb\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mset_trace\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# <-- 这个表示添加断点\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m     \u001b[0maddition\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0madd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnum2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     16\u001b[0m     \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0maddition\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     17\u001b[0m     \u001b[0msubtraction\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msub\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnum2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/opt/conda/envs/python35-paddle120-env/lib/python3.7/bdb.py\u001b[0m in \u001b[0;36mtrace_dispatch\u001b[0;34m(self, frame, event, arg)\u001b[0m\n\u001b[1;32m     86\u001b[0m             \u001b[0;32mreturn\u001b[0m \u001b[0;31m# None\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     87\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mevent\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'line'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 88\u001b[0;31m             \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdispatch_line\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mframe\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     89\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mevent\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'call'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     90\u001b[0m             \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdispatch_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mframe\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0marg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/opt/conda/envs/python35-paddle120-env/lib/python3.7/bdb.py\u001b[0m in \u001b[0;36mdispatch_line\u001b[0;34m(self, frame)\u001b[0m\n\u001b[1;32m    111\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstop_here\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mframe\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbreak_here\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mframe\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    112\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0muser_line\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mframe\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 113\u001b[0;31m             \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mquitting\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mBdbQuit\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    114\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtrace_dispatch\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    115\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mBdbQuit\u001b[0m: "
     ]
    }
   ],
   "source": [
    "import pdb\r\n",
    "import sys\r\n",
    "\r\n",
    "def add(num1=0, num2=0):\r\n",
    "    return int(num1) + int(num2)\r\n",
    "    \r\n",
    "def sub(num1=0, num2=0):\r\n",
    "    return int(num1) - int(num2)\r\n",
    "    \r\n",
    "def main():\r\n",
    "    #Assuming our inputs are valid numbers\r\n",
    "    num1 = 33\r\n",
    "    num2 = 44\r\n",
    "    pdb.set_trace() # <-- 这个表示添加断点\r\n",
    "    addition = add(num1, num2)\r\n",
    "    print (addition)\r\n",
    "    subtraction = sub(num1, num2)\r\n",
    "    print (subtraction)\r\n",
    "    \r\n",
    "if __name__ == '__main__':\r\n",
    "    main()\r\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "一旦开始运行, 会出现交互框\n",
    "\n",
    "如下图所示: \n",
    "\n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/6e50855df42e478898e8d34f8048d01d9d3c9b3a17e04ed4a432ec032130907d)\n",
    "\n",
    "在这个输入框里敲入命令, 即可开始调试. \n",
    "\n",
    "通常这些命令都是一个字母, 因此毋庸担心. \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 3. 下一行 - > n\n",
    "\n",
    "在输入框里, 输入n, 可转到下一行\n",
    "\n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/08a9fb56feb44f41999fd9b7e1f42b02df264b48ccc4495e8a12d11da5a6c2db)\n",
    "\n",
    "这将执行当前代码行，现在可以执行下一行。\n",
    "\n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/545d1fd617c148b0afb5325c06a34fa00f945b06e5f54ae0aab144eed28e2ac8)\n",
    "\n",
    "我们可以使用n逐行遍历整个程序，但这不会非常有用。\n",
    "\n",
    "另外你可能已经注意到pdb实际上没有进入我们的add函数。让我们看看更多选项，使调试更有趣。\n",
    "\n",
    "注意：\n",
    "另一个很酷的功能是点击'返回键'执行你以前的命令（在这种情况下只是n）。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 4. 打印 - > p\n",
    "\n",
    "让我们再次开始调试我们的程序。（你可以点击c使pdb跳到最后或直到下一个断点。由于我们没有任何程序将完成执行）。\n",
    "\n",
    "现在，如果我们想知道num1或num2包含什么，我们可以在输入框里输入：\n",
    "```\n",
    "p num1\n",
    "```\n",
    "\n",
    "如截图所示:\n",
    "\n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/684676cd8e5346e0bc7ba08004a47d882d6f134ab8d147628245f497e84bc0f5)\n",
    "\n",
    "回车\n",
    "\n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/2e950c54bdf149d89ad0f802261246186e0c652eeefa4b34b5ef801c4d60936e)\n",
    "\n",
    "\n",
    "这非常方便查看我们的变量实际存储的值。\n",
    "\n",
    "现在让我们进入添加功能。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 5. 动态添加断点 - > b\n",
    "\n",
    "我们在运行程序之前使用了pdb.set_trace（）来设置断点。\n",
    "\n",
    "我们通常希望在调试会话开始后在程序的特定位置添加断点。\n",
    "\n",
    "这个时候可以考虑输入b. 可以亲自试一下. \n",
    "\n",
    "# 6. 动态分配变量\n",
    "知道在调试会话期间可以分配变量以帮助调试也很有用。考虑：\n",
    "\n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/fa65a33c9d4e49bc9e53d385dc04312ebbb2015698744ea8b0659d6da516edb1)\n",
    "\n",
    "输入后运行\n",
    "\n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/ca3a74f79ae14aad87baee29a613307d4ce917c5bbb5404fb90ec2ecbde7dd3a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 7. 退出 - > q\n",
    "最后，如果你想在任何时候退出，你可以使用q。正在执行的程序被中止。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 8. 其他\n",
    "\n",
    "还有一种更好的方式, 叫ipdb. 用法和PDB很相似. 不过返回的输出是彩色的. \n",
    "* ENTER (重复上次命令)\n",
    "* c (继续)\n",
    "* l (查找当前位于哪里)\n",
    "* s (进入子程序,如果当前有一个函数调用，那么 s 会进入被调用的函数体)\n",
    "* n(ext)  让程序运行下一行，如果当前语句有一个函数调用，用 n 是不会进入被调用的函数体中的\n",
    "* r (运行直到子程序结束)\n",
    "* !<python 命令>\n",
    "* h (帮助)\n",
    "* a(rgs) 打印当前函数的参数\n",
    "* j(ump) 让程序跳转到指定的行数\n",
    "* l(ist) 可以列出当前将要运行的代码块\n",
    "* p(rint) 最有用的命令之一，打印某个变量\n",
    "* q(uit) 退出调试\n",
    "* r(eturn) 继续执行，直到函数体返回\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Collecting ipdb\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/20/b7/d32c6a1706f58779db553ff78f59cd3fc3a627d87901b80cd67a4f264a89/ipdb-0.13.5.tar.gz\n",
      "Requirement already satisfied: setuptools in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from ipdb) (41.4.0)\n",
      "Collecting ipython>=7.17.0 (from ipdb)\n",
      "\u001b[?25l  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/3b/43/6dbd0610550708fc418ad027fda97b5f415da9053749641654fdacfec93f/ipython-7.21.0-py3-none-any.whl (784kB)\n",
      "\u001b[K     |████████████████████████████████| 788kB 25.8MB/s eta 0:00:01\n",
      "\u001b[?25hRequirement already satisfied: decorator in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from ipython>=7.17.0->ipdb) (4.4.0)\n",
      "Requirement already satisfied: pexpect>4.3; sys_platform != \"win32\" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from ipython>=7.17.0->ipdb) (4.7.0)\n",
      "Requirement already satisfied: pickleshare in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from ipython>=7.17.0->ipdb) (0.7.5)\n",
      "Requirement already satisfied: backcall in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from ipython>=7.17.0->ipdb) (0.1.0)\n",
      "Requirement already satisfied: pygments in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from ipython>=7.17.0->ipdb) (2.4.2)\n",
      "Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from ipython>=7.17.0->ipdb) (2.0.10)\n",
      "Collecting jedi>=0.16 (from ipython>=7.17.0->ipdb)\n",
      "\u001b[?25l  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/f9/36/7aa67ae2663025b49e8426ead0bad983fee1b73f472536e9790655da0277/jedi-0.18.0-py2.py3-none-any.whl (1.4MB)\n",
      "\u001b[K     |████████████████████████████████| 1.4MB 44.0MB/s eta 0:00:01\n",
      "\u001b[?25hRequirement already satisfied: traitlets>=4.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from ipython>=7.17.0->ipdb) (4.3.3)\n",
      "Requirement already satisfied: ptyprocess>=0.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pexpect>4.3; sys_platform != \"win32\"->ipython>=7.17.0->ipdb) (0.6.0)\n",
      "Requirement already satisfied: wcwidth in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython>=7.17.0->ipdb) (0.1.7)\n",
      "Requirement already satisfied: six>=1.9.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython>=7.17.0->ipdb) (1.12.0)\n",
      "Collecting parso<0.9.0,>=0.8.0 (from jedi>=0.16->ipython>=7.17.0->ipdb)\n",
      "\u001b[?25l  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ad/f0/ef6bdb1eba2dbfda60c985cd8d7b47b6ed8c6a1f5d212f39ff50b64f172c/parso-0.8.1-py2.py3-none-any.whl (93kB)\n",
      "\u001b[K     |████████████████████████████████| 102kB 50.6MB/s ta 0:00:01\n",
      "\u001b[?25hRequirement already satisfied: ipython-genutils in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from traitlets>=4.2->ipython>=7.17.0->ipdb) (0.2.0)\n",
      "Building wheels for collected packages: ipdb\n",
      "  Building wheel for ipdb (setup.py) ... \u001b[?25ldone\n",
      "\u001b[?25h  Created wheel for ipdb: filename=ipdb-0.13.5-cp37-none-any.whl size=11243 sha256=56409c41ba4aa3652e478c6972ee1babd73d79547c55c3c2cd0155aa1071569c\n",
      "  Stored in directory: /home/aistudio/.cache/pip/wheels/39/a5/9a/ed8b2154d210935161415db5fea2d10080bf8d52da50018a31\n",
      "Successfully built ipdb\n",
      "Installing collected packages: parso, jedi, ipython, ipdb\n",
      "  Found existing installation: parso 0.5.1\n",
      "    Uninstalling parso-0.5.1:\n",
      "      Successfully uninstalled parso-0.5.1\n",
      "  Found existing installation: jedi 0.15.1\n",
      "    Uninstalling jedi-0.15.1:\n",
      "      Successfully uninstalled jedi-0.15.1\n",
      "  Found existing installation: ipython 7.0.1\n",
      "    Uninstalling ipython-7.0.1:\n",
      "      Successfully uninstalled ipython-7.0.1\n",
      "Successfully installed ipdb-0.13.5 ipython-7.21.0 jedi-0.18.0 parso-0.8.1\n"
     ]
    }
   ],
   "source": [
    "#如发现环境中未安装, 可以运行下方代码来安装ipdb\n",
    "\n",
    "!pip install ipdb -i https://pypi.tuna.tsinghua.edu.cn/simple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> \u001b[0;32m<ipython-input-5-a1bcd79d01c2>\u001b[0m(15)\u001b[0;36mmain\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;32m     14 \u001b[0;31m    \u001b[0mipdb\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mset_trace\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# <-- 这个表示添加断点\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0m\u001b[0;32m---> 15 \u001b[0;31m    \u001b[0maddition\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0madd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnum2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0m\u001b[0;32m     16 \u001b[0;31m    \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0maddition\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "import ipdb\n",
    "import sys\n",
    "\n",
    "def add(num1=0, num2=0):\n",
    "    return int(num1) + int(num2)\n",
    "    \n",
    "def sub(num1=0, num2=0):\n",
    "    return int(num1) - int(num2)\n",
    "    \n",
    "def main():\n",
    "    #Assuming our inputs are valid numbers\n",
    "    num1 = 33\n",
    "    num2 = 44\n",
    "    ipdb.set_trace() # <-- 这个表示添加断点\n",
    "    addition = add(num1, num2)\n",
    "    print (addition)\n",
    "    subtraction = sub(num1, num2)\n",
    "    print (subtraction)\n",
    "    \n",
    "if __name__ == '__main__':\n",
    "    main()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PaddlePaddle 1.7.2 (Python 3.5)",
   "language": "python",
   "name": "py35-paddle1.2.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
